#ifndef Tui_TBase_h
#define Tui_TBase_h

#include "../PreInclude.h"
#include "../DevMacros.h"
#include "cocos2d.h"
#include "cocos-ext.h"
USING_NS_CC;
USING_NS_CC_EXT;

using namespace rapidxml;




int readAttrInt(xml_node<> * pItem,const char* cAttr);
float readAttrFloat(xml_node<> * pItem,const char* cAttr);
void readAttrString(xml_node<> * pItem,const char* cAttr, string & ret);
CCSize readContainSize(xml_node<> * pItem,CCNode* parent);



class TTip;
class TPanelItem;

struct TuiEvent{
public:
    virtual void procTuiEvent(const string&,TPanelItem*){};
    
};

class TPanelItem:public CCNodeRGBA{
public:
    CREATE_FUNC(TPanelItem)
    
    TPanelItem();
    bool init();
    
    virtual void selected(); 
    virtual void unselected(); 
    
    void setLeft(int n); 
    void setRight(int n); 
    void setTop(int n); 
    void setBottom(int n);
    void setHorizontalCenter(int n);
    void setVerticalCenter(int n); 
    
    void setX(int x); 
    virtual void setY(int y);
    
    virtual void initWidthConf(xml_node<> * pItem);
    
    virtual void updateGame(float dt = 0.0);
    
    static CCNode* tipContainer;
    
    DEV_PROPERTY(TTip*, m_pTip, Tip); 
    DEV_PROPERTY(bool , m_bDirty, Dirty);
    
    virtual  TPanelItem* getGuideItem( int gid){return NULL;}
    string tId;
    string tType;
    string tEvent;
    string tMask;
    int tMaskType;
    int nPage;
    void showTip(); 
    
    void hideTip();
    
    virtual void updateDisplayedOpacity(GLubyte parentOpacity);
protected: 
    
};

class TButton;
class TLabel;
class TPage;
class TScale9;
class TTab;
class TImage;
class TScrollPanel;
class TCount;
class TVBox;
class TBar;
class TCheckBox;
class TRocker;
class TEditBar;
class TInput;
class TRadio;
class TDropdown;
class TAccordion;
class TStrokeLabel;

class TPanel: public TPanelItem ,public TuiEvent, public CCTouchDelegate
{
public:
    virtual  ~TPanel();
    TPanel();
     
    
    virtual bool init();
    CREATE_FUNC(TPanel)
    void loadConf();
    void loadConf(const string&);
    void parseConf(xml_node<> * pItem);
    TPanelItem* getUI(const string& );
    virtual  void initWidthConf(xml_node<> * pItem);
    virtual void procTuiEvent(const string&,TPanelItem*);
    
    virtual void onCreateComplete();
    virtual void onEnter();
    virtual void onExit();
    
    CCNode* getItemByPos(int nX,int nY);
    void addItem(CCNode*);
    void addChildItem(CCNode*);
    void removeItem(CCNode*);
    virtual  void clearItems();
    CCArray* getItems(){return  gTouchItems;}
    CCNode* getSelectedItem(){return  m_pSelectedItem;}
    int getSelectedIndex();
    void setSelecetIndex(int idex);
    void setSelecetItem(CCNode* node);
    void clearSelect();
    
    DEV_PROPERTY(bool, m_bTouchEnable, TouchEnable);
    
    virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
    virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
    virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
    virtual void ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent);
    virtual void registerWithTouchDispatcher();
    
    virtual void setTarget(CCObject *rec, SEL_MenuHandler selector);
    
    string confPath;
    TScrollPanel* scrollParent;
    
    TEditBar* getEditBar(const char*);
    TInput* getInput(const char*);
    TScrollPanel* getScrollPanel(const char*);
    TButton* getButton( const char*);
    TLabel* getLabel(const char*);
    TImage* getImage(const char*);
    TPage* getPage(const char*);
    TScale9* getScale9(const char*);
    TTab* getTab(const char*);
    TCount* getCount(const char*);
    TPanel* getPanel(const char*);
    TVBox* getVBox(const char*);
    TBar* getBar(const char*);
    
    TCheckBox* getCheckBox(const char*);
    TRocker* getRocker(const char*);
    TRadio* getRadio(const char*);
    TStrokeLabel* getStrokeLabel(const char*);
    
    TDropdown* getDropdown(const char*);
    TAccordion* getAccordion(const char*);
    
    bool hasLoadConf;
    bool hasInitMask;
    virtual void onSelectItem(CCObject*);
	virtual void onUnSelectItem(CCNode*);
    
    static void setStaticTarget(CCObject *rec, SEL_MenuHandler selector);
protected:
    static  CCObject*     m_pStaticListener;
    static  SEL_MenuHandler  m_pStaticfnSelector;

    void initMask();
    CCObject*       m_pListener;
    SEL_MenuHandler    m_pfnSelector;
    
    CCObject*       m_pMoveListener;
    SEL_MenuHandler    m_pfnMoveSelector;
    
    
    CCNode* m_pSelectedItem;
    CCPoint touchStartPos; 
    CCPoint touchMovePos;
    CCArray* gTouchItems;
    map<string,TPanelItem*> gUIs;
};

#endif
